extends CharacterBody2D

var speed: float = 40.0
var direction: int = 1
var gravity: float = 400.0

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_2d: RayCast2D = $RayCast2D


func _physics_process(delta: float) -> void:
    if not is_on_floor():
        velocity.y += gravity * delta
    else:
        velocity.x = speed * direction

    move_and_slide()

    if not ray_cast_2d.is_colliding() or is_on_wall():
        direction *= -1
        ray_cast_2d.position *= -1
    
    if direction == 1:
        animated_sprite_2d.flip_h = false
    else:
        animated_sprite_2d.flip_h = true